//
//  SYEmoticonCollectionViewCell.swift
//  YANAWeibo
//
//  Created by mac on 16/5/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYEmoticonCollectionViewCell: UICollectionViewCell {
    
    // 保存表情button
    private lazy var emoticonButtonsArr: [SYEmoticonButton] = [SYEmoticonButton]()
    
    /// 表情模型数组
    var emtions: [SYEmoticon]? {
        didSet {
            guard let ets = emtions else {
                return
            }
            // 取出重用问题
            for allBtn in emoticonButtonsArr {
                allBtn.hidden = true
            }
            // 遍历赋值
            for (i, value) in ets.enumerate() {
                
                let button = emoticonButtonsArr[i]
                button.emoticon = value
            }
        }
    }
    
    /// 删除按钮
    private lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteEmoticonButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 给item 添加 20 个button
        addChildButton()
        contentView.addSubview(deleteEmoticonButton)
    }
    
    // 添加表情控件
    private func addChildButton() {
        for _ in 0..<20 {
            let btn = SYEmoticonButton()
            btn.addTarget(self, action: "emoticonBtnClick:", forControlEvents: .TouchUpInside)
            btn.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(btn)
            emoticonButtonsArr.append(btn)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 九宫格布局
        let itemWidth = width / 7
        let itemHeight = height / 3
        
        for (i, value) in emoticonButtonsArr.enumerate() {
            value.size = CGSize(width: itemWidth, height: itemHeight)
            // 计算col的索引
            let col = i % 7
            // 计算row的索引
            let row = i / 7
            
            value.x = CGFloat(col) * itemWidth
            value.y = CGFloat(row) * itemHeight
        }
        
        // 设置删除按钮
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeight)
        deleteEmoticonButton.x = width - itemWidth
        deleteEmoticonButton.y = height - itemHeight
    }
    
     // MARK: --监听点击事件
    @objc private func emoticonBtnClick(btn: SYEmoticonButton) {
        let emoticon = btn.emoticon
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticon)
    }
    
    @objc private func deleteEmoticonButtonClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedDeleteEmotiocnButtonNotification, object: nil)
    }
  
}
